import axios from 'axios'
export default{
  state() {
    return{
      list:[]
    }
  },
  mutations:{
    changeList(state,list){
      state.list = list
    }
  },
  actions:{
    async getList ({commit}) {
      const res = await axios('http://192.168.87.184:3000/cart')
      console.log(res,888)
      commit('changeList',res.data)
    },
    async syncCount({commit},obj) {
      const res = await axios.patch('http://192.168.87.184:3000/cart/'+ obj.id, {
        count: obj.count
      })
    }
  },
  getters: {
    total(state) {
      return state.list.reduce((sum,item)=>sum+item.count,0)
    },
    totalPrice(state) {
      return state.list.reduce((sum,item)=>sum+item.count*item.price,0)
    }
  },
  namespaced:true
}